package com.ruoyi.web.countDay;


import cn.hutool.http.HttpUtil;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.domain.Holiday;

import java.util.*;

public class webToDays {
    String line;

    public String getYear(int year) {
        String strUrl = "https://timor.tech/api/holiday/year/" + String.valueOf(year);
        line = HttpUtil.get(strUrl);
        return line;
    }

    public int count(int year, int mon) {
        int num = 0;
        try {
            ArrayList<Holiday> holidays = new ArrayList<Holiday>();
            ArrayList<Integer> date = new ArrayList<Integer>();
            ArrayList<Integer> bus = new ArrayList<Integer>();
            JSONObject jsonObject = JSONObject.parseObject(line);
            JSONObject holiday = (JSONObject) jsonObject.get("holiday");

            for (String s : holiday.keySet()) {
                String[] split = s.split("-");
                if (mon == Integer.parseInt(split[0])) {
                    JSONObject h = (JSONObject) holiday.get(s);
                    com.ruoyi.system.domain.Holiday holiday1 = new Holiday();
                    holiday1.setHoliday((Boolean) h.get("holiday"));
                    holiday1.setName((String) h.get("name"));
                    holiday1.setWage((Integer) h.get("wage"));
                    holiday1.setTarget((String) h.get("target"));
                    holiday1.setDate((String) h.get("date"));
                    String[] dates = ((String) h.get("date")).split("-");
                    holidays.add(holiday1);
                    if ((Boolean) h.get("holiday")) {
                        date.add(Integer.parseInt(dates[2]));
                    } else {
                        bus.add(Integer.parseInt(dates[2]));
                    }
                }
            }
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, year);
            // 不设置的话默认为当年
            calendar.set(Calendar.MONTH, mon - 1);
            // 设置月份
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            // 设置为当月第一天
            int daySize = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            // 当月最大天数
            for (int i = 1; i <= daySize; i++) {
                if (!date.contains(i)) {
                    int week = calendar.get(Calendar.DAY_OF_WEEK);
                    //周末休息
                    if ((week == Calendar.SATURDAY || week == Calendar.SUNDAY)) {
                        // 1代表周日，7代表周六 判断这是一个星期的第几天从而判断是否是周末
                        //如果是补班，则属于正常上班时间
                        if (bus.contains(i)) {
                            num++;
                        }
                    } else {
                        num++;
                    }
                }
//在第一天的基础上加1
                calendar.add(Calendar.DATE, 1);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }

    public static void main(String[] args) {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        System.out.println(year);
        webToDays t = new webToDays();
        System.out.println(Calendar.YEAR);
        ArrayList<Integer> days = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            int num = t.count(2020, i);
            if (num != 0) {
                days.add(num);
            }
        }
        int i = 1;
        for (Integer day : days) {
            System.out.println(i + "月有" + day + "个工作日");
            i++;
        }
    }
}
